﻿[assembly: System.Web.UI.TagPrefix("GSC.WebControls", "GSC")]
namespace GSC.WebControls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design.WebControls;
    using System.Drawing.Design;
    using System.Drawing;
    using System.Web.UI.Design;
    using System.ComponentModel;
    using ComponentModel.Design;
    /// <summary>
    /// 管理后台版权控件。
    /// </summary>
    /// <remarks>管理后台版权控件。</remarks>
    [ToolboxItemFilter("GSC.WebControls", ToolboxItemFilterType.Custom)]
    [ToolboxData("<{0}:BackgroundCopyrightControl runat=server height=51px width=100%></{0}:BackgroundCopyrightControl>")]
    [SRDescription("Manage_Copyright", typeof(Properties.Resources))]
    [Designer(typeof(BackgroundCopyrightControlDesigner))]
    public class BackgroundCopyrightControl : CompositeControl, INamingContainer, ICompositeControlDesignerAccessor
    {
        /// <summary>
        /// 获取或设置背景图片。
        /// </summary>
        /// <remarks>获取或设置背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_Copyright_BackImage", typeof(Properties.Resources))]
        [Category("Appearance")]
        public string BackImage
        {
            get
            {
                object o = this.ViewState["BackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["BackImage"] = value;
            }
        }

        /// <summary>
        /// </summary>
        protected override string TagName
        {
            get
            {
                return "div";
            }
        }

        /// <summary>
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
        ITemplate _contentTemplate;
        /// <summary>
        /// 获取或设置内容模板对象。
        /// </summary>
        /// <remarks>获取或设置内容模板对象。</remarks>
        /// <value>System.Web.UI.ITemplate</value>
        [
                      Browsable(false),
                      PersistenceMode(PersistenceMode.InnerDefaultProperty),
                      DefaultValue(typeof(ITemplate), null),
                      Description(""),
                      TemplateContainer(typeof(BackgroundCopyrightControl))
                      ]
        public ITemplate ContentTemplate
        {
            get
            {
                return _contentTemplate;
            }
            set
            {
                _contentTemplate = value;
            }
        }

        /// <summary>
        /// </summary>
        /// <param name="w"></param>
        protected override void AddAttributesToRender(HtmlTextWriter w)
        {
            base.AddAttributesToRender(w);
        }

        /// <summary>
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Clear();
            CreateControlHierarchy();
            ClearChildViewState();
        }

        /// <summary>
        /// </summary>
        protected virtual void CreateControlHierarchy()
        {
            if (this._contentTemplate == null) this._contentTemplate = new DefaultEmptyTemplate();
            _contentTemplate.InstantiateIn(this);
        }
        /// <summary>
        /// 设计时获取资源文件地址。
        /// </summary>
        /// <remarks>设计时获取资源文件地址。</remarks>
        /// <returns>string</returns>
        public virtual string GetWebResourceUrl(string resourceUrl)
        {
            if (this.Page == null) return string.Empty;
            if (this.Page.ClientScript == null) return string.Empty;
            return this.Page.ClientScript.GetWebResourceUrl(typeof(BackgroundCopyrightControl), "GSC.WebControls.BackgroundCopyrightControl.Images." + resourceUrl);
        }

        void ICompositeControlDesignerAccessor.RecreateChildControls()
        {
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.Style["overflow"] = "hidden";
            this.Style["background-position"] = "center";
            this.Style["background-repeat"] = "no-repeat";
            if (string.IsNullOrEmpty(this.BackImage))
                this.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("BackgroundCopyrightControl.gif")) + ")";
            else
                this.Style["background-image"] = "url(" + this.ResolveClientUrl(this.BackImage) + ")";
        }
        /// <summary>
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            this.EnsureChildControls();
            //
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
            base.Render(writer);
        }
    }
}